﻿using System;
using System.Collections.Generic;
using CommonLibrary.Extensions;
using CommonLibrary.Models.Profiles;

namespace CommonLibrary.Logic
{
    public class ProfileConfigGenerator
    {
        public ApplicationProfileConfig GenerateNewProfile(string applicationName)
        {
            ApplicationProfileConfig applicationProfileConfig = new ApplicationProfileConfig();

            applicationProfileConfig.Description = applicationName;

            Profile profile = new Profile();
            profile.Id = 0;

            profile.ButtonsMap = GenerateGenericButtonMap();
            profile.AxisMap = GenerateGenericAxisMap();

            applicationProfileConfig.Profiles.Add(profile);

            return applicationProfileConfig;
        }

        private List<CustomAction> GenerateGenericButtonMap()
        {
            List<CustomAction> buttonMap = new List<CustomAction>();
            foreach (Button button in Enum.GetValues(typeof(Button)))
            {
                buttonMap.Add(new CustomAction()
                                          {
                                              Name = button.ToString(),
                                              Trigger = new List<ActionEvent>()
                                                            {
                                                                button.GetGenericActionForButton()
                                                            }
                                          });
            }
            return buttonMap;
        }

        private List<CustomAction> GenerateGenericAxisMap()
        {
            List<CustomAction> axisMap = new List<CustomAction>();
            foreach (Axis axis in Enum.GetValues(typeof(Axis)))
            {
                axisMap.Add(new CustomAction()
                                        {
                                            Name = axis.ToString(),
                                            Trigger = new List<ActionEvent>()
                                                            {
                                                                axis.GetGenericActionForAxis()
                                                            }
                                        });
            }
            return axisMap;
        }

        public enum Button
        {
            //17 buttons
            Select,
            Start,
            PS,

            Triangle,
            Square,
            Cross,
            Circle,

            Left, // DPad
            Right,
            Up,
            Down,

            L1,
            R1,
            L2, // Analog triggers
            R2,
            L3,
            R3
        }

        public enum Axis
        {
            LeftX,
            LeftY,
            RightX,
            RightY
        }
    }
}
